
//
//  JJCreateHtml.m
//  CreateHtml
//
//  Created by iOS on 2017/10/16.
//  Copyright © 2017年 iOS. All rights reserved.
//

#import "JJCreateHtml.h"
@interface JJCreateHtml ()
@property (nonatomic, copy) NSString *htmlStr;
@end

@implementation JJCreateHtml
+ (instancetype)getInstance {
    return [[JJCreateHtml alloc] init];
}


- (NSString *) createHtml:(NSString *) htmlStr {
    _htmlStr = htmlStr;
    //生成随机文件名
    NSString *name = [[NSUUID UUID] UUIDString];
    //得到路劲
    NSString *htmlPath = [self htmlPathName:name];
    return htmlPath;
}

//创建文件路劲
- (NSString *) htmlPathName:(NSString *) name {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *saveDirectory = [paths objectAtIndex:0];
    NSString *saveFileName = [NSString stringWithFormat:@"%@.html",name];
    
    NSString *filepath = [saveDirectory stringByAppendingPathComponent:saveFileName];
    //保存路劲
    [self savePath:filepath];
    
    [_htmlStr  writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    return filepath;
}

// 持久化储存路劲
- (void) savePath:(NSString *) path {
    NSString *savePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *saveFileName = [savePath stringByAppendingPathComponent:@"htmlPathSave.plist"];

    NSArray *result = [NSArray arrayWithContentsOfFile:saveFileName];
    if (result) {
        NSMutableArray *resultMutable = [NSMutableArray arrayWithArray:result];
        [resultMutable addObject:path];
        [resultMutable writeToFile:saveFileName atomically:YES];
    } else {
        NSArray *pathResult = [NSArray arrayWithObject:path];
        [pathResult writeToFile:saveFileName atomically:YES];
    }
    
}

@end
